<?php
/*
 * do_resize函数
 * 将upload目录下特定文件名的图片进行变换，存入upload/resized目录下
 * 不维持长宽比时格式为原文件名_高度x宽度.原扩展名
 * 维持长宽比时将以 $master_dim 为主要参数（为空时默认为宽度），格式为原文件名_h高度.原扩展名/原文件名_w宽度.原扩展名
 * 输入：
 * $location 文件路径 e.g. upload/1234.jpg
 * $height 高度
 * $width 宽度
 * $maintain_ratio 是否维持长宽比，请输入true和false
 * $master_dim 维持长宽比的主轴
 */
function do_resize($location, $width, $height, $maintain_ratio, $master_dim) {
    $object =& get_instance();
    $object->load->library('image_lib');
    if( !is_dir('upload/resized') ) {
        mkdir('upload/resized', 0777, TRUE); //判断缩略图文件夹是否存在，不存在则创建
    }    
    $config['image_library'] = 'gd2';
    $config['source_image'] = $location;
    $config['new_image'] = 'upload/resized/';    
    $config['maintain_ratio'] = $maintain_ratio;
    $config['create_thumb'] = TRUE;
    $thumb = _gen_thumb($width, $height, $maintain_ratio, $master_dim);
    $config['thumb_marker'] = $thumb;
    $config['width'] = $width;
    $config['height'] = $height;
    $config['master_dim'] = ($master_dim == 'height') ? 'height' : 'width';
    
    $object->image_lib->initialize($config);
    $object->image_lib->resize();
    return _gen_resized_location($location, $thumb);
}

function crop_and_resize_cover($post_id, $location, $width, $height, $x_axis, $y_axis) {
    $object =& get_instance();
    $object->load->library('image_lib');
    if( !is_dir('upload/cover') ) {
        mkdir('upload/cover', 0777, TRUE); //判断封面文件夹是否存在，不存在则创建
    }
    
    foreach( glob('upload/cover/'.$post_id.'.*') as $file ) {
        unlink($file);
    }    //删除过去生成过的封面
    
    $image_info = pathinfo($location);
    $config['image_library'] = 'gd2';
    $config['source_image'] = $location;
    $config['new_image'] = 'upload/cover/'.$post_id.'.'.$image_info['extension'];
    $config['width'] = $width;
    $config['height'] = $height;
    $config['x_axis'] = $x_axis;
    $config['y_axis'] = $y_axis;
    $config['master_dim'] = 'width';
    $object->image_lib->initialize($config);
    $object->image_lib->crop();
    
    return $config['new_image'];    //返回生成的封面的地址
}

function photo_resize($location, $width, $height, $maintain_ratio, $master_dim) {
    $thumb = _gen_thumb($width, $height, $maintain_ratio, $master_dim);
    $resized_location = _gen_resized_location($location, $thumb);
    if (file_exists($resized_location)) {
        return $resized_location;
    } else {
        do_resize($location, $width, $height, $maintain_ratio, $master_dim);
        return $resized_location;
    }
}

function _gen_thumb($width, $height, $maintain_ratio, $master_dim) {
    if( !$maintain_ratio ) {
        $thumb = '_' . $width . 'x' . $height;
    } else {
        if ($master_dim == 'height') {
            $thumb = '_h' . $height;
        } else {
            $thumb = '_w' . $width;
        }
    }
    return $thumb;
}

function _gen_resized_location($location, $thumb) {
    $file_name = substr($location, strpos($location, '/') + 1);
    $file_type = substr($file_name, strrpos($file_name,'.'));
    $file_name_only = substr($file_name, 0, strrpos($file_name , '.'));    
    return 'upload/resized/' . $file_name_only . $thumb . $file_type;
}
?>